<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img src="@/assets/logo.svg" alt="logo" class="logo" />
            <div class="title">梦之泡泡</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="150px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                {{ loginUserStore.loginUser.userName ?? loginUserStore.loginUser.userAccount }}
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/mySpace">
                      <UserOutlined />
                      我的空间
                    </router-link>
                  </a-menu-item>

                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { LogoutOutlined, MailOutlined, UserOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useUserLoginStore.ts'
import { userLogoutUsingGet } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()

// 菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(MailOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManagePage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManagePage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/picture/addPicture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },

  {
    key: 'others',
    label: h(
      'a',
      { href: 'https://blog.csdn.net/suhuixiaoxin?spm=1000.2115.3001.5343', target: '_blank' },
      '我的博客',
    ),
    title: '我的博客',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

const router = useRouter()
// 当前高亮菜单项
const current = ref<string[]>([])
// 监听路由变化，更新菜单高亮
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})
// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}
// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingGet()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>
<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.logo {
  height: 45px;
}

.title {
  margin-left: 10px;
  color: black;
  font-size: 18px;
}
</style>
